package controllers;

import java.util.*;

import domain.Game;
import domain.Player;
import domain.Tournament;
import domain.models.MyListModel;
import domain.models.RegisteredPlayersTableModel;

import persistence.PersistenceController;

public class DomainController {
	private PersistenceController persistenceController;

	private Tournament currentTournament;

	public DomainController() {
		persistenceController = PersistenceController.getInstance();
	}
	
	public PersistenceController getPersistenceController()
	{
		return persistenceController;
	}
	
	public boolean isTournamentNameUnique(String name) {
		return persistenceController.isTournamentNameUnique(name);
	}

	public List<Game> pairUp() {
		return currentTournament.pairUp();
	}

	public void makeNewTournament(String name) {
		currentTournament = new Tournament(name);
		
		
	}
	
	public void configureParametersPairUpMethod(int gamesAgoCondition, int placesApartCondition)
	{
		Tournament.setMinimumAmountOfGamesAgo2PlayersLastPlayedAgainstEachOther(gamesAgoCondition);
		Tournament.setMaximumAmountOfPlaces2PlayersApartOfEachOther(placesApartCondition);
	}

	public String[] getMostRecentTournaments() {
		return persistenceController.getMostRecentTournaments();
	}

	public void openTournament(String name) {
		currentTournament = persistenceController.openTournament(name);
	}

	
	
	public void addPlayers(String names)
	{
		currentTournament.addPlayers(names);
	}
	
	public void savePlayer(String[] nameBefore, String[] nameAfter)
	{
		currentTournament.savePlayer(nameBefore, nameAfter);
	}
	
	public void removePlayer(String[] name)
	{
		currentTournament.removePlayer(name);
	}
	
	
	public void addPlayersToBePaired(String names)
	{
		currentTournament.addPlayersToBePaired(names);
	}

	public void removePlayerToBePaired(String name) {
		currentTournament.removePlayersToBePaired(name);
	}
	
	

	public List<Game> showGamesInProgress() {
		List<Game> allGamesInTournament = currentTournament.getGames();
		List<Game> gamesInProgress = new ArrayList<Game>();
		
		for(Game g : allGamesInTournament)
		{
			if(g.isGameInProgress())
				gamesInProgress.add(g);
		}
		return gamesInProgress;
	}

	public void saveResult(int gameID, String[] result, boolean inProgress) {
		currentTournament.saveResult(gameID, result, inProgress);
	}

	public List<Player> showRanking() {
		return currentTournament.getPlayersAccordingToRanking();
	}

	public List<Game> showGamesPlayed() {
		List<Game> allGamesInTournament = currentTournament.getGames();
		List<Game> gamesPlayed = new ArrayList<Game>();
		
		for(Game g : allGamesInTournament)
		{
			if(!g.isGameInProgress())
				gamesPlayed.add(g);
		}
		return gamesPlayed;
	}

	public void saveTournament() {
		persistenceController.saveTournament(currentTournament);
	}
	
	public List<Player> getRegisteredPlayers()
	{
		/*
		List<Player> registeredPlayers = currentTournament.getPlayers();
		registeredPlayers.removeAll(currentTournament.getPlayersToBePaired());
		String[] registeredPlayersS = new String[registeredPlayers.size()];
		for(int counter = 0; counter < registeredPlayers.size(); counter++)
		{
			registeredPlayersS[counter] = registeredPlayers.get(counter).getFirstname() + " " +
								registeredPlayers.get(counter).getLastname();
		}
		*/
		return currentTournament.getRegisteredPlayers();
	}
	
	//TODO
	public String[] getPlayersToBePaired()
	{
		List<Player> playersToBePaired = currentTournament.getPlayersToBePaired();
		String[] playersToBePairedS = new String[playersToBePaired.size()];
		for(int counter = 0; counter < playersToBePaired.size(); counter ++)
		{
			playersToBePairedS[counter] = playersToBePaired.get(counter).getFirstname() + " " +
								playersToBePaired.get(counter).getLastname();
		}
		return playersToBePairedS;
	}
	
	public Tournament getCurrentTournament()
	{
		return currentTournament;
	}
	
	
	public RegisteredPlayersTableModel getRegisteredPlayersTableModel()
	{
		return new RegisteredPlayersTableModel(this);
	}
	
	public MyListModel getMyListModel()
	{
		return new MyListModel(this);
	}
}